package com.stm.toolsScrap.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stm.common.annotation.DataScope;
import com.stm.common.core.text.Convert;
import com.stm.common.exception.BusinessException;
import com.stm.common.utils.DateUtils;
import com.stm.toolsScrap.domain.KfRToolsScrap;
import com.stm.toolsScrap.domain.KfToolsScrapApply;
import com.stm.toolsScrap.mapper.KfRToolsScrapMapper;
import com.stm.toolsScrap.mapper.KfToolsScrapApplyMapper;
import com.stm.toolsScrap.service.KfToolsScrapApplyService;

/**
 * 
 * @Description 报废impl
 * @CreateTime 2019年4月4日 上午8:43:59
 * @CreateBy 林龙
 */
@Service
public class KfToolsScrapApplyServiceImpl implements KfToolsScrapApplyService
{
    @Autowired
    private KfToolsScrapApplyMapper kfToolsScrapApplyMapper;
    @Autowired
    private KfRToolsScrapMapper kfRToolsScrapMapper;
    

    /**
     * 根据条件分页查询数据
     * 
     * @param kfTools 信息
     * @return list集合信息
     */
    @Override
    @DataScope(tableAlias = "b3")
    public List<KfToolsScrapApply> selectAllPageList(KfToolsScrapApply kfToolsScrapApply){
        return kfToolsScrapApplyMapper.selectAllPageList(kfToolsScrapApply);
    }

    /**
     * 根据id查询信息
     * 
     * @param id 
     * @return 对象信息
     */
    @Override
    public KfToolsScrapApply selectByPrimaryKey(String id){
        return kfToolsScrapApplyMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增保存信息
     * 
     * @param kfToolsScrapApply RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insert(KfToolsScrapApply kfToolsScrapApply){
    	UUID uuid = UUID.randomUUID();
    	kfToolsScrapApply.setId(uuid.toString());
    
    	kfToolsScrapApply.setCreateTime(DateUtils.getNowDate());
    	kfToolsScrapApply.setApplyDate(DateUtils.getNowDate());
    	kfToolsScrapApply.setStatus("1");
    	
    	//工具试验申请表
    	kfToolsScrapApplyMapper.insertSelective(kfToolsScrapApply);
    	
    	
    	String [] kfToolsList=kfToolsScrapApply.getTbToolss().split(",");
    	
    	//新增工具试验关系表
    	for (String kfTools : kfToolsList) {
    		KfRToolsScrap kfRToolsScrap=new  KfRToolsScrap();
    		
    		kfRToolsScrap.setScrapId(uuid.toString());
    		kfRToolsScrap.setToolsId(kfTools);
    		kfRToolsScrap.setStatus("1");
    		
    		kfRToolsScrapMapper.insertSelective(kfRToolsScrap);
		}
    	
    	return 1;
    }

	/**
     * 修改保存信息
     * 
     * @param kfToolsScrapApply RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int update(KfToolsScrapApply kfToolsScrapApply){
    	return  kfToolsScrapApplyMapper.updateByPrimaryKeySelective(kfToolsScrapApply);
    }
    
    /**
     * 批量删除
     * 
     * @param ids
     * @throws Exception
     */
    @Override
    public int deleteByPrimary(String ids) throws BusinessException
    {
        String[] kfToolsScrapApplyIds = Convert.toStringArray(ids);
        for (String id : kfToolsScrapApplyIds) {
        	KfToolsScrapApply kfToolsScrapApply=kfToolsScrapApplyMapper.selectByPrimaryKey(id);
        	if(kfToolsScrapApply.getStatus().equals("2") || kfToolsScrapApply.getStatus().equals("3") ){
        		 throw new BusinessException("不允许删除审核通过的数据");
        	}
		}
        
        kfRToolsScrapMapper.deleteByPrimary(kfToolsScrapApplyIds);
        
        return kfToolsScrapApplyMapper.deleteByPrimary(kfToolsScrapApplyIds);
    }

    /**
     * 
     * @Description 审核
     * @CreateTime 2019年4月11日 下午2:45:46
     * @CreateBy 林龙
     * @param ids
     * @return
     * @throws Exception
     */
	@Override
	public int reviewByPrimary(String ids, String status, String rejectedReason, String userName) throws Exception {
		
		String[] kfToolsScrapApplyIds = Convert.toStringArray(ids);
		
		List<KfToolsScrapApply> list =new ArrayList<KfToolsScrapApply>(); //批量更新
		for (String id : kfToolsScrapApplyIds) {
			
			KfToolsScrapApply kfToolsScrapApply =new KfToolsScrapApply();
			kfToolsScrapApply.setId(id);
			kfToolsScrapApply.setStatus(status);
			kfToolsScrapApply.setAuditUser(userName);
			kfToolsScrapApply.setAuditDate(DateUtils.getNowDate());
			kfToolsScrapApply.setRejectedReason(rejectedReason);
			list.add(kfToolsScrapApply);
			
			//当状态为申请通过时
			if(status.equals("2")){
				List<KfRToolsScrap> kfRToolsScrapList=kfRToolsScrapMapper.selectMaind(id);
				kfRToolsScrapMapper.updateByPrimary(kfRToolsScrapList);
				
			}
			
		}
		
		
		
		
		return kfToolsScrapApplyMapper.reviewByPrimary(list);
		  
	}

	/**
	 * 
	 * @Description 查询报废审核数量
	 * @CreateTime 2019年4月16日 上午9:10:34
	 * @CreateBy 林龙
	 * @return
	 */
	@Override
	@DataScope(tableAlias = "b2")
	public int selectByScrappedNums(KfToolsScrapApply kfToolsScrapApply) {
		return kfToolsScrapApplyMapper.selectByScrappedNums(kfToolsScrapApply);
	}

}
